<template>
  <div class="content">

    <div class="page-content">
      <div class="page-content-top">
        <Step :step="['创建名片','实名认证','授权认证','认证审核']" :active="2"></Step>
      </div>
      <div class="page-content-bottom">
        <div class="page-content-bottom-title">
          <span class="bottom-text">
            实名认证
          </span>
          <br/>
          <span class="bottom-data">
            扫描二维码进行人脸实名认证
          </span>
        </div>
        <table>
          <tr>
            <td></td>
          </tr>
        </table>
        <div class="page-content-bottom-title">
          <div class="page-content-bottom-fill">
            <span class="required">*</span><span>真实姓名</span>
            <input v-model="form.realName" class="form-ctrl code" maxlength="5" type="text"
                   placeholder="请填写你的真实姓名"/>
            <br>
          </div>
          <div class="page-content-bottom-fill">
            <span class="required">*</span><span>身份证号</span>
            <input v-model="form.idCard" class="form-ctrl code" maxlength="18" type="text"
                   placeholder="请输入18位身份证号码"/>
          </div>

        </div>
      </div>
      <div class="page-content-confirm" @click="qrCode">开始人脸识别认证</div>
      <div class="page-content-agreement">
        <input v-model="form.check" type="checkbox"/>
        <span style="padding-left: 10px">我已阅读并同意 </span>
        <a href="/public#/authentication" style="color: #ff7626">《实名认证及在职认证服务协议》</a>
      </div>
    </div>

    <el-dialog title="扫描进入小程序完成认证" v-if="dialogVisible" :visible.sync="dialogVisible" width="20%"
               :close-on-click-modal="false" :close-on-press-escape="false" :destroy-on-close="true">
      <img style="padding-left: 25px;width: 325px" :src=codeSrc>
      <br/>
      <span>小程序端人脸识别认证通过后点击确定</span>
      <span slot="footer" class="dialog-footer">
      <el-button plain type="primary" @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import Step from "@/components/Step/Step";
import {getCode, toRealNameExamine} from "@/api/enterprise";
import {idCardValidate} from "@/utils/idCardValidate";
import {validate} from "@/utils/validate";

export default {
  components: {Step},
  data() {
    return {
      dialogVisible: false,
      form: {
        realName: '',
        idCard: '',
        check:false,
      },
      codeSrc:'',
    }
  },
  methods: {
    qrCode() {
      const rules = {
        realName: {
          name: '真实姓名',
          required: true,
        },
        idCard: {
          name: '身份证号',
          required: true,
          validator: function (value, form, callback) {
            let res = idCardValidate(value);
            if (!res.result) {
              callback(res.msg)
            }
            callback()
          }
        },
      };
      if(!this.form.check){
        this.$message.error('请先阅读并勾选同意协议');
        return;
      }
      if(validate(this.form,rules)){
        const loading=this.$loading({text:'加载中'})
        getCode(this.form).then((res)=>{
          this.codeSrc=res;
          this.dialogVisible = true;
        }).finally(()=>{
          loading.close()
        })
      }
    },
    submit() {
      const loading=this.$loading({text:'加载中'})
      this.$store.dispatch('user/getUser').then((res)=>{
        if(res){
          if (res.authenticationStatus==1){
            this.$message.warning('未验证或验证失败');
            return;
          }
         else {
            this.$message.success('验证成功');
            this.$router.push({name:'Home'})
          }
        }else{
          this.$message.warning('未验证或验证失败');
        }
      }).finally(()=>{
        loading.close()
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.content {
  .el-dialog {

    width: 25%;
  }

  .page-content {
    background-color: #FFFFFF;
    width: $contentWidth;
    min-height: $fullMinHeight;
    margin: 20px auto;
    flex-direction: column;
    border-radius: 5px;
    height: 710px;

    .page-content-top {
      margin: 0 30px 0 30px;
      padding-top: 25px;
      border-bottom: 1px solid #DBDBDB;

    }

    .page-content-bottom {

      .page-content-bottom-title {
        margin: 50px 30px 0 30px;

        .page-content-bottom-fill {
          padding-top: 30px;

          .form-ctrl {
            width: 100%;
            height: 40px;
            padding: 0 20px;
            background: none;
          }

          .form-ctrl.code {
            margin-left: 30px;
            width: 50%;
            border: 1px solid #DBDBDB;
            border-radius: 10px;
          }

          .required {
            color: red;
          }
        }
      }

      .bottom-text {
        font-size: 25px;
      }

      .bottom-data {
        font-size: 14px;
        color: #72767b;
      }
    }

    .page-content-confirm {
      margin: 40px 50px 30px 132px;
      border-radius: 10px;
      width: 170px;
      height: 35px;
      background: #ff7626;
      color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
    }

    .page-content-agreement {
      margin: 40px 50px 30px 132px;

    }
  }
}
</style>
